using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using hedefgrup.reports.renault;
using hedefgrup.reports.renault.date;
using hedefgrup.reports.renault.database;
using System.Text;

public partial class adm_dataExport : reportPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();
        base.ensureAdministratorIsLoggedIn();

        reportMonth yearAndMonth = null;
        bool exportAll = false;
        try
        {
            yearAndMonth = (exportAll = base.getString("m") == "ALL") ? null : base.getReportMonth();
        }
        catch
        {
            Response.Write("<select id='m' style='width:250px;' onchange=\"document.location.href='?m=' + this.value\"><option value=''>Please select a month</option><option value='ALL'>Export all data to single file</option>");
            foreach (reportMonth available in base.getDataCache().availableData)
            {
                Response.Write(string.Format("<option value='{0}'>{1} {2}</option>", available.dateValue, available.getYear(), available.getMonthName()));
            }
            return;
        }

        if (!exportAll)
        {
            bool requestedDataExists = false;

            foreach (reportMonth availableMonth in base.getDataCache().availableData)
            {
                if (availableMonth.dateValue == yearAndMonth.dateValue)
                {
                    requestedDataExists = true;
                    break;
                }
            }

            if (!requestedDataExists)
            {
                Response.Write("No data is available for " + yearAndMonth.dateValue.ToString());
                return;
            }
        }

        DataTable monthlyData = db.query(string.Format("SELECT * FROM " + base.getDataCache().dataTableName + " {0} ORDER BY [ID] ASC", exportAll ? "" : "WHERE AY = @p_0 AND YIL = @p_1"), exportAll ? null : new object[] { yearAndMonth.getMonth(), yearAndMonth.getYear() });

        Response.ContentType = "text/plain";
        Response.AppendHeader("content-disposition", "attachment;filename=" + (base.userProjectName.ToUpperInvariant()) + "_" + (exportAll ? "ALL_DATA" : (yearAndMonth.getMonth().ToString("00") + "_" + yearAndMonth.getYear().ToString("0000"))) + "_EXPORT.txt");

        // Response.Buffer = false;
        DataColumn lastColumn = monthlyData.Columns[monthlyData.Columns.Count - 1];

        foreach (DataColumn dc in monthlyData.Columns)
        {
            Response.Write(dc.ColumnName + (dc.Equals(lastColumn) ? "" : "\t"));
        }

        Response.Write("\r\n");

        foreach (DataRow dr in monthlyData.Rows)
        {
            foreach (DataColumn dc in monthlyData.Columns)
            {
                Response.Write(Convert.ToString(dr[dc]).Trim() + (dc.Equals(lastColumn) ? "" : "\t"));
            }
            Response.Write("\r\n");
        }

        monthlyData.Dispose();
        monthlyData = null;

    }
}
